# coding: utf-8
import sys

from PySide6.QtGui import QColor
from PySide6.QtWidgets import QApplication
from qfluentwidgets import isDarkTheme, Theme

from common import isWin11, cfg
from .title_bar import CustomTitleBar

if isWin11():
    from qframelesswindow import AcrylicWindow as Window
else:
    from qframelesswindow import FramelessWindow as Window


class FramelessWindow(Window):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setTitleBar(CustomTitleBar(self))
        self.titleBar.raise_()

        if sys.platform == "darwin":
            self.setSystemTitleBarButtonVisible(True)
            self.titleBar.minBtn.hide()
            self.titleBar.maxBtn.hide()
            self.titleBar.closeBtn.hide()

        cfg.themeChanged.connect(self.updateBackground)
        self.updateBackground()
        self.enabledCenterDesktop()

    def enabledCenterDesktop(self):
        self.setMinimumSize(1100, 650)
        desktop = QApplication.primaryScreen().availableGeometry()
        w, h = desktop.width(), desktop.height()
        self.move(w // 2 - self.width() // 2, h // 2 - self.height() // 2)

    def updateBackground(self, theme: Theme = None):
        self.windowEffect.setMicaEffect(self.winId(), isDarkMode=isDarkTheme())
        if not isWin11():
            color = QColor(25, 33, 42) if isDarkTheme() else QColor(240, 244, 249)
            self.setStyleSheet(f"FramelessWindow{{background: {color.name()}}}")
